package com.xxl.job.kettle.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.kettle.constant.Constant;
import com.xxl.job.kettle.core.service.BaseService;

import java.util.Map;

/**
 * 实现功能：
 *
 * @author 作者[zk]
 * @version 1.0.00  2022-11-01 10:42
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    @Override
    public IPage<T> getPage(Map<String, Object> params) {
        long curPage = 1;
        long limit = 10;
        if(null != params.get(Constant.PAGE)){
            curPage = Long.parseLong((String) params.get(Constant.PAGE));
        }
        if(null != params.get(Constant.LIMIT)){
            limit = Long.parseLong((String) params.get(Constant.LIMIT));
        }

        Page<T> page = new Page<>(curPage, limit);
        String orderField = (String) params.get(Constant.ORDER_FIELD);
        String order = (String) params.get(Constant.ORDER);
        if(StrUtil.isNotBlank(orderField) && StrUtil.isNotBlank(order)){
            if(Constant.ASC.equalsIgnoreCase(order)){
                page.addOrder(OrderItem.asc(orderField));
            }else{
                page.addOrder(OrderItem.desc(orderField));
            }
        }
        return page;
    }
}
